﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment_8
{
    public partial class Form1 : Form
    {
        private static ConnectDB cDB;
        private string checkAction = ""; // Biến kiểm tra nút Save dành cho Action nào (Add, Update, Delete)
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DisableCaption(); // Ẩn form
            cDB = new ConnectDB();

            cDB.loadDataToGrid(this.dgvCustomer);
            // dgvCustomer.ClearSelection();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableCaption(); // Bật các caption
            // Ẩn các caption Add, Update, Delete, Exit
            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnExit.Enabled = false;

            ResetForm(); // Xóa trắng form nhập l liệu
            checkAction = "Add";
            dgvCustomer.Enabled = false; 
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            EnableCaption(); // Bật các caption
            // Ẩn các caption Add, Update, Delete, Exit
            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnExit.Enabled = false;
            dgvCustomer.Enabled = false; ;
            checkAction = "Update";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = dgvCustomer.SelectedRows[0].Index;
            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu này chứ?", "Customer Details", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                cDB.Delete(index);
            }

            btnCancel_Click(sender, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (checkAction == "Add")
            {
                if (!ValidateInput()) // kiểm tra nhập liệu
                {
                    return;
                }

                cDB.Add(GetDataInput());
                cDB.loadDataToGrid(this.dgvCustomer);
                btnCancel_Click(sender, e);
                return;
            }

            if (checkAction == "Update")
            {
                int index = dgvCustomer.SelectedRows[0].Index;
                cDB.Update(GetDataInput(), index);
                btnCancel_Click(sender, e);
                return;
            }

            if (checkAction == "Delete")
            {

                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DisableCaption();
            ResetForm();
            btnAdd.Enabled = true;
            btnExit.Enabled = true;

            checkAction = "";
            FillDataToForm(); // Check xem grid có dòng đang chọn không, nếu có thì fill data vào
            dgvCustomer.Enabled = true;
            dgvCustomer.ClearSelection();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắn chắn muốn thoát?", "Customer Details", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void DisableCaption()
        {
            gbCustomerDetails.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;

            dgvCustomer.ClearSelection();
        }

        private void EnableCaption()
        {
            gbCustomerDetails.Enabled = true;
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnExit.Enabled = true;

            dgvCustomer.Enabled = true; 
        }

        private void ResetForm()
        {
            // Textbox
            txtName.Text = "";
            txtAge.Text = "";
            txtAddress.Text = "";
            txtProblem.Text = "";
            txtEmail.Text = "";

            // Combo box
            cbStatus.SelectedIndex = 0;

            // Date time picker
            dtpDate.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            dtpDateSolved.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
        }

        private void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Validate nhập liệu phải là số hoặc control
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private Boolean ValidateInput()
        {
            if (txtName.Text.Trim() == "")
            {
                lblCheckName.Visible = true;
                lblCheckName.Text = "Enter name";
                txtName.Focus();
                return false;
            }
            lblCheckName.Visible = false;

            if (txtAge.Text.Trim() == "")
            {
                lblCheckAge.Visible = true;
                lblCheckAge.Text = "Enter age";
                txtAge.Focus();
                return false;
            }
            lblCheckAge.Visible = false;

            if (txtAddress.Text.Trim() == "")
            {
                lblCheckAddress.Visible = true;
                lblCheckAddress.Text = "Enter address";
                txtAddress.Focus();
                return false;
            }
            lblCheckAddress.Visible = false;

            if (txtEmail.Text.Trim() == "")
            {
                lblCheckEmail.Visible = true;
                lblCheckEmail.Text = "Enter email";
                txtEmail.Focus();
                return false;
            }
            lblCheckEmail.Visible = false;

            if (txtProblem.Text.Trim() == "")
            {
                lblCheckProblem.Visible = true;
                lblCheckProblem.Text = "Enter name";
                txtProblem.Focus();
                return false;
            }
            lblCheckProblem.Visible = false;

            return true;
        }

        private List<object> GetDataInput()
        {
            List<object> lstInput = new List<object>();
            string name = txtName.Text.Trim().ToString();
            string age = txtAge.Text.Trim();
            string address = txtAddress.Text.Trim();
            string email = txtEmail.Text.Trim();
            string problem = txtProblem.Text.Trim();
            string status = (cbStatus.SelectedIndex == 0) ? "True" : "False"; // Nếu chọn Ok thì gán giá trị 1, ngược lại = 0
            string date = dtpDate.Text;
            string dateSolved = dtpDateSolved.Text;

            lstInput.Add(name);
            lstInput.Add(age);
            lstInput.Add(address);
            lstInput.Add(email);
            lstInput.Add(problem);
            lstInput.Add(status);
            lstInput.Add(date);
            lstInput.Add(dateSolved);
            
            return lstInput;
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            FillDataToForm();
        }

        private void FillDataToForm()
        {
            if (dgvCustomer.CurrentCell == null)
            {
                return;
            }
            try
            {
                int index = dgvCustomer.SelectedRows[0].Index; // Lấy dòng đc chọn
                if (index < 0)
                {
                    return;
                }

                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
                txtName.Text = dgvCustomer.Rows[index].Cells[1].Value.ToString();
                txtAge.Text = dgvCustomer.Rows[index].Cells[2].Value.ToString();
                txtAddress.Text = dgvCustomer.Rows[index].Cells[3].Value.ToString();
                txtEmail.Text = dgvCustomer.Rows[index].Cells[4].Value.ToString();
                txtProblem.Text = dgvCustomer.Rows[index].Cells[5].Value.ToString();
                cbStatus.SelectedIndex = (dgvCustomer.Rows[index].Cells[6].Value.ToString().Equals("True")) ? 0 : 1;
                dtpDate.Text = dgvCustomer.Rows[index].Cells[7].Value.ToString();
                dtpDateSolved.Text = dgvCustomer.Rows[index].Cells[8].Value.ToString();
            }
            catch (Exception)
            {

            }
        }

        private void dgvCustomer_SelectionChanged(object sender, EventArgs e)
        {
           // FillDataToForm();
        }
    }
}
